package com.allen.excelUtils;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.allen.excelUtils.column.Column;
import com.allen.excelUtils.row.RowObject;

public class ExcelExporter {

	private static HSSFSheet createSheet(String sheetName){
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet(sheetName);
		return sheet;
	}
	
	public static HSSFWorkbook export(String sheetName,ExcelConfig config,List<RowObject> rows){
		HSSFSheet sheet = createSheet(sheetName);
		int startIndex = (config.isWithOutTableHead())?0:1;
		if (config.isWithOutTableHead()) {
			initColumnTitle(sheet,config.getColumns());
		}
		if (null == rows || rows.size() == 0) {
			return sheet.getWorkbook();
		}
		
		for (RowObject row : rows) {
			
		}
		
		return sheet.getWorkbook();
	}
	
	public static HSSFWorkbook exportByMap(String sheetName,ExcelConfig config,List<Map<String, String>> datas) {
		HSSFSheet sheet = createSheet(sheetName);
		
		int startIndex = (config.isWithOutTableHead())?0:1;
		if (config.isWithOutTableHead()) {
			initColumnTitle(sheet,config.getColumns());
		}
		if (null == datas || datas.size() == 0) {
			return sheet.getWorkbook();
		}

		HSSFRow row = null;
		HSSFCell cell=null;
		List<Column> columns = config.getColumns();
		for (Map<String, String> rowData : datas) {
			row = sheet.createRow(startIndex);
			if(config.isDisplayNameFirst()){
				if (columns == null || columns.isEmpty()) {
					throw new RuntimeException("column内容不能为空。");
				}
				//按照第一行顺序取值
				for (int colIndex = 0; colIndex < columns.size(); colIndex++) {
				    cell = row.createCell(colIndex);
					cell.setCellValue(new HSSFRichTextString(rowData
							.get(columns.get(colIndex).getName())));
				}
			}else {
				 //无视第一行列排列规则，循环取值
				 int index = 0;
				 for (Entry<String, String>  column: rowData.entrySet()) {
					 cell = row.createCell(index);
					 cell.setCellValue(new HSSFRichTextString(column.getValue()));
					 index++;
				 }
			}
			startIndex++;
		}
		
		return sheet.getWorkbook();
	}
	
	/**
	 * 依照配置构建列标题
	 * @param sheet
	 * @param columns
	 */
	private static void initColumnTitle(HSSFSheet sheet,List<Column> columns){
		HSSFRow row = sheet.createRow(0);
		for (int index = 0; index < columns.size(); index++) {
			HSSFCell cell = row.createCell(index);
			/*cell.setCellStyle(style);*/
			cell.setCellValue(new HSSFRichTextString(columns.get(index).getAlias()));
		}
		
	}
	
	/*
	private Map<String, HSSFCellStyle> styleMap = new HashMap<String, HSSFCellStyle>();
	private void initStyle(HSSFWorkbook workbook){
		// 模型关键属性样式
		HSSFCellStyle mainAttrStyle = workbook.createCellStyle();
		mainAttrStyle.setFillForegroundColor(HSSFColor.YELLOW.index);
		mainAttrStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		mainAttrStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		mainAttrStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		mainAttrStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
		mainAttrStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
		mainAttrStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
		HSSFFont fontm = workbook.createFont();
		fontm.setColor(HSSFColor.RED.index);
		fontm.setFontHeightInPoints((short) 10);
		mainAttrStyle.setFont(fontm);
		styleMap.put(GlobalConstants.STYLE_MODEL_MAIN, mainAttrStyle);

		// 生成一个样式
		HSSFCellStyle headStyle = workbook.createCellStyle();
		// 设置这些样式
		headStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
		headStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		headStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		headStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		headStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
		headStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
		headStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		// 生成一个字体
		HSSFFont font = workbook.createFont();
		font.setColor(HSSFColor.VIOLET.index);
		font.setFontHeightInPoints((short) 10);
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		// 把字体应用到当前的样式
		headStyle.setFont(font);

		styleMap.put(GlobalConstants.STYLE_HEAD, headStyle);
		// 生成并设置另一个样式
		HSSFCellStyle dataStyle = workbook.createCellStyle();
		dataStyle.setFillForegroundColor(HSSFColor.WHITE.index);
		dataStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		dataStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		dataStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		dataStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
		dataStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
		dataStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
		dataStyle.setVerticalAlignment(HSSFCellStyle.ALIGN_LEFT);
		// 生成另一个字体
		HSSFFont font2 = workbook.createFont();
		font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
		// 把字体应用到当前的样式
		dataStyle.setFont(font2);
		styleMap.put(GlobalConstants.STYLE_DATA, dataStyle);
		styleMap.put(GlobalConstants.STYLE_MODEL_EXPEND, dataStyle);
	}
	public HSSFWorkbook exportModel(List<RowObject> rsList,String sheetTitle){
		
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet(sheetTitle);
		
		if(null == rsList || rsList.size() < 1)
			return workbook;
		
		// 设置表格默认列宽度为15个字节
		sheet.setDefaultColumnWidth(20);
		
		initStyle(workbook);
		for (int i = 0; i < rsList.size(); i++) {
			RowObject ro = rsList.get(i);
			HSSFRow row = sheet.createRow(i);
			HSSFCellStyle style = styleMap.get(ro.getStyle());
			for (int j = 0; j < ro.getDataList().size(); j++) {
				HSSFCell cell = row.createCell(j);
				cell.setCellStyle(style);
				HSSFRichTextString text = new HSSFRichTextString(ro
						.getDataList().get(j));
				cell.setCellValue(text);
			}
		}
		return workbook;
	}*/
}
